﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        static string conString=ConfigurationManager.ConnectionStrings["conString"].ToString();
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
        #region NguoiDung
        //lay danh sach nguoi dung
        public DataSet layDanhSachNguoiDung()
        {
            String sql = "Select * from NguoiDung nd where nd.TinhTrang=1";
            SqlConnection cnn = new SqlConnection(conString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            return ds;
        }
        //them nguoi dung
        public string themNguoiDung(string tenNguoiDung, string matKhau, DateTime thoiGianDangKy, string email, int tinhTrang, int maLoaiNguoiDung)
        {
            String sql = "Insert into NguoiDung values (N'" + tenNguoiDung + "', N'" + matKhau + "', '" + thoiGianDangKy + "', N'" + email + "','" + 1 + "','" + 2 + "')";
            SqlConnection cnn = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(sql, cnn);
            try
            {
                if (cnn.State.ToString() == "Closed")
                {
                    cnn.Open();
                }
                if (cmd.ExecuteNonQuery() == 0)
                {
                    return "Thêm thất bại";
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            return "Thêm thành công";
        }
        //xoa nguoi dung
        //sua nguoi dung
        #endregion
        #region DiaDiem
        //lay danh sach dia diem
        public DataSet layDanhSachDiaDiem()
        {
            String sql = "Select * from DiaDiem dd where dd.TinhTrang=1";
            SqlConnection cnn = new SqlConnection(conString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            return ds;
        }
        //them dia diem
        public string themDiaDiem(string tenDiaDiem, string diaChi, int maPhuong, int maQuan, float kinhDo, float viDo, string ghiChu, int maNguoiDung, int tinhTrang)
        {
            String sql = "Insert into DiaDiem values (N'" + tenDiaDiem + "', N'" + diaChi + "','" + maQuan + "','" + maPhuong + "','"+kinhDo+"','"+viDo+"',N'"+ghiChu+"','"+maNguoiDung+"','"+tinhTrang+"')";
            SqlConnection cnn = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(sql, cnn);
            try
            {
                if (cnn.State.ToString() == "Closed")
                {
                    cnn.Open();
                }
                if (cmd.ExecuteNonQuery() == 0)
                {
                    return "Thêm thất bại";
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            return "Thêm thành công";
        }
        //xoa dia diem
        public string xoaDiaDiem(int maDiaDiem)
        {
            string sql = "Delete from DiaDiem where maDiaDiem='" + maDiaDiem + "'";

            SqlConnection cnn = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(sql, cnn);
            try
            {
                if (cnn.State.ToString() == "Closed")
                {
                    cnn.Open();
                }
                if (cmd.ExecuteNonQuery() == 0)
                {
                    return "Xóa thất bại";
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            return "Xóa thành công";
        }
        //sua dia diem
        public string suaDiaDiem(int maDiaDiem, string tenDiaDiem, string diaChi, int maPhuong, int maQuan, float kinhDo, float viDo, string ghiChu, int maNguoiDung, int tinhTrang)
        {
            SqlConnection cnn = new SqlConnection(conString);

            string sql = "Update DiaDiem set";
            sql += " TenDiaDiem= N'" + tenDiaDiem + "',";
            sql += " MaPhuong ='" + maPhuong + "',";
            sql += " MaQuan ='" + maQuan + "',";
            sql += " KinhDo = '" + kinhDo + "',";
            sql += " ViDo = " + viDo + ",";
            sql += " GhiChu = '" + ghiChu + "'";
            sql += " MaNguoiDung = '" + maNguoiDung + "'";
            sql += " TinhTrang = '" + tinhTrang + "'";
            sql += " Where MaDiaDiem='" + maDiaDiem + "'";

            SqlCommand cmd = new SqlCommand(sql, cnn);
            try
            {
                if (cnn.State.ToString() == "Closed")
                {
                    cnn.Open();
                }
                if (cmd.ExecuteNonQuery() == 0)
                {
                    return "Cập nhật thất bại";
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            return "Cập nhật thành công";
        }
        #endregion
    }
}
